package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderService orderService;


    @Autowired
    private ReportService reportService;

    /**
     * 统计分析-会员数量折线图
     * @return
     */
    @GetMapping("/getMemberReport")
    private Result getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
        }
        map.put("months", months);
        List<Integer> memberCount = new ArrayList<>();
        memberCount = memberService.countByMonth(months);
        map.put("memberCount", memberCount);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    /**
     * 统计分析-套餐预约占比饼形图
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        List<Map<String, Object>> setmealCount = orderService.countBySetmeal();
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {
            setmealNames.add(String.valueOf(map.get("name")));
        }
        // 构造返回值
        Map<String, Object> map = new HashMap<>();
        map.put("setmealName", setmealNames);
        map.put("setmealCount", setmealCount);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    /**
     * 统计分析-运营数据页面展示
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();
        map = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, map);
    }
}
